﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frmChitieudanhgiahanhkiem : Form
    {
        public frmChitieudanhgiahanhkiem()
        {
            InitializeComponent();
        }
        cls_DM_Chitieudanhgiahanhkiem objChitieudanhgia = new cls_DM_Chitieudanhgiahanhkiem();
        private void frmChitieudanhgiahanhkiem_Load(object sender, EventArgs e)
        {
            GetAll_Caphoc();
        }
        private void GetAll_Caphoc()
        {
            gridLookUpEdit1.Properties.DataSource = objChitieudanhgia.GetAll_Caphoc();
            gridLookUpEdit1.Properties.DisplayMember = "TenCapHoc";
            gridLookUpEdit1.Properties.ValueMember = "CapHocID";
        }
        private void GetAll_Chitieudanhgia(int ch)
        {
            gridControl1.DataSource = objChitieudanhgia.GetAll_Chitieudanhgiahanhkiem(ch);
        }
        int caphocid = 0;
        private void grvCaphoc_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                DataRow dtr = grvCaphoc.GetDataRow(e.RowHandle);
                caphocid = (int)dtr["CapHocID"];
                GetAll_Chitieudanhgia(caphocid);
            }
            catch (Exception ex) { throw ex; }

        }

        private void Save_Chitieudanhgia()
        {
            clsDM_Chitieudanhgiahanhkiem objdmctdghk = new clsDM_Chitieudanhgiahanhkiem();
            objdmctdghk.TenChiTieuDanhGiaHanhKiem = gridView1.GetFocusedRowCellValue("TenChiTieuDanhGiaHanhKiem").ToString();
            objdmctdghk.FK_CapHocID = caphocid;
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmctdghk.ChiTieuDanhGiaHanhKiemID = 1;
                objdmctdghk.isChangeable = false;
                blnResult = objChitieudanhgia.Insert(objdmctdghk);
            }
            else
            {
                objdmctdghk.ChiTieuDanhGiaHanhKiemID = (int)gridView1.GetFocusedRowCellValue("ChiTieuDanhGiaHanhKiemID");
                objdmctdghk.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objChitieudanhgia.update(objdmctdghk);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
                gridLookUpEdit1.Enabled = true;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Chitieudanhgia();
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenChiTieuDanhGiaHanhKiem").ToString()) == DialogResult.No)
                return;
            objChitieudanhgia.delete((int)gridView1.GetFocusedRowCellValue("ChiTieuDanhGiaHanhKiemID"));
            GetAll_Chitieudanhgia(caphocid);
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
            gridLookUpEdit1.Enabled = false;
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Chitieudanhgia(caphocid);
            gridLookUpEdit1.Enabled = true;
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
            gridLookUpEdit1.Enabled = false;
        }


    }
}
